<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="file" id="file">
    <img src="" alt="">
    <button id="btn">上传</button>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.10.0/jquery.js"></script>
    <script>
        let formData = new FormData();
        $("#file").change(function(){
            let file = this.files[0];
            formData.append('img',file);
            let fd = new FileReader();
            fd.readAsDataURL(file);
            fd.onload = function(){
                $("img").attr('src',this.result);
            }
        })
        /*
        ajax默认传不了文件
        FormData
        */
        $("#btn").click(function(){
           
            $.ajax({
                url:'/upload',
                type:'POST',
                dataType:'json',
                data:formData,
                processData: false, //必须 缓存关了
                contentType: false, //必须 关闭 设置 数据格式（以 数据本身格式为准 xxxxx urlencoded）
                success:function(res){
                    console.log(res)
                }
            })
        })
    </script>
</body>
</html>